package com.yonyou.cyxdms.sysAuth.service.service.carfaccompanymaintenance;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.sysAuth.service.dto.carfaccompanymaintenance.CarFacCompanyMaintenanceTreeDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.CarFactoryBasicinfoPO;
import com.yonyou.cyxdms.sysAuth.service.entity.ComOrganizationPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.CarFactoryBasicinfoMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.ComOrganizationMapper;
import com.yonyou.cyxdms.sysAuth.service.vo.carfaccompanymaintenance.DealerBasicinfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 车厂公司维护
 * 
 * @author lxj
 * @date 2018年12月11日
 */
@Service
@SuppressWarnings("rawtypes")
public class CarFacCompanyMaintenanceServiceImpl implements CarFacCompanyMaintenanceService {

	@Autowired
	CarFactoryBasicinfoMapper carFactoryBasicinfoMapper;
	@Autowired
	ComOrganizationMapper comOrganizationMapper;

	/**
	 * 车厂公司维护控制类  DealerBasicinfoVO
	 *
	 * @author dengqiang
	 * @since 2019/7/18
	 */
	@Override
	public IPage<DealerBasicinfoVO> queryDealerBasicinfo(Page<DealerBasicinfoVO> page, Map<String, Object> queryCondition) {

		List<DealerBasicinfoVO> voList = carFactoryBasicinfoMapper.pageDealerBasicinfo(page,queryCondition);
		page.setRecords(voList);
		return  page;
	}
	/**
	 * 车厂公司维护控制类新增
	 *
	 * @author dengqiang
	 * @since 2019/7/22
	 */
	@Override
	public Integer addOrg(CarFacCompanyMaintenanceTreeDTO carFacCompanyMaintenanceTreeDTO) {

		List<Map<String,Object>> result = carFactoryBasicinfoMapper.checkCodeOrName(carFacCompanyMaintenanceTreeDTO.getCompanyCode(),carFacCompanyMaintenanceTreeDTO.getDealerName());

        if (!CommonUtils.isNullOrEmpty(result)) {
			throw new ServiceBizException("当前公司代码或名称已存在，请重新填写！");
		}else {

			CarFactoryBasicinfoPO factorybasicinfopo = new CarFactoryBasicinfoPO();
			factorybasicinfopo.setCompanyCode(carFacCompanyMaintenanceTreeDTO.getCompanyCode());
			factorybasicinfopo.setDealerName(carFacCompanyMaintenanceTreeDTO.getDealerName());
			factorybasicinfopo.setDealerShortname(carFacCompanyMaintenanceTreeDTO.getDealerShortName());
			factorybasicinfopo.setProvince(carFacCompanyMaintenanceTreeDTO.getProvince());

			if(!StringUtils.isNullOrEmpty(carFacCompanyMaintenanceTreeDTO.getCity())) {
				factorybasicinfopo.setCity(carFacCompanyMaintenanceTreeDTO.getCity());
			}

			factorybasicinfopo.setZipCode(carFacCompanyMaintenanceTreeDTO.getZipCode());
			factorybasicinfopo.setFax(carFacCompanyMaintenanceTreeDTO.getFax());
			factorybasicinfopo.setHotLine(carFacCompanyMaintenanceTreeDTO.getHotLine());
			factorybasicinfopo.setAddress(carFacCompanyMaintenanceTreeDTO.getAddress());
			factorybasicinfopo.setDealerStatus(Integer.parseInt(carFacCompanyMaintenanceTreeDTO.getDealerStatus()));
			carFactoryBasicinfoMapper.insert(factorybasicinfopo);

			Long dealerId = factorybasicinfopo.getDealerId();

			//公司组织
			ComOrganizationPO comorganizationpo = new ComOrganizationPO();
			comorganizationpo.setCompanyId(dealerId.intValue());
			comorganizationpo.setOrgType(16701001);
			comorganizationpo.setOrgCode(carFacCompanyMaintenanceTreeDTO.getCompanyCode());
			comorganizationpo.setOrgName(carFacCompanyMaintenanceTreeDTO.getDealerName());
			comorganizationpo.setStatus(Integer.parseInt(carFacCompanyMaintenanceTreeDTO.getDealerStatus()));
			comOrganizationMapper.insert(comorganizationpo);

			return dealerId.intValue();
		}
	}

	@Override
	public void modifyComOrg(String ids, CarFacCompanyMaintenanceTreeDTO orgDto) {

		if("10011002".equals(orgDto.getDealerStatus())) {
			//查询当前公司是否有下级
			List<Map<String,Object>> result = carFactoryBasicinfoMapper.isInferior(ids);
            if (!CommonUtils.isNullOrEmpty(result)) {
				throw new ServiceBizException("当前公司存在下级，不允许设为无效！");
			}else {
                toModifyComOrg(ids, orgDto);
			}
		}else {
            toModifyComOrg(ids, orgDto);
        }
    }

    public void toModifyComOrg(String ids, CarFacCompanyMaintenanceTreeDTO orgDto) {
        //判断名称是否重复
        List<Map<String, Object>> list = carFactoryBasicinfoMapper.isRepeatName(orgDto.getDealerName());
        if (!CommonUtils.isNullOrEmpty(list)) {
            throw new ServiceBizException("当前公司名称已存在，请重新填写！");
        } else {

            CarFactoryBasicinfoPO carfactorybasicinfopo = carFactoryBasicinfoMapper.selectById(ids);
            carfactorybasicinfopo.setCompanyCode(orgDto.getCompanyCode());
            carfactorybasicinfopo.setDealerName(orgDto.getDealerName());
            carfactorybasicinfopo.setDealerShortname(orgDto.getDealerShortName());
            carfactorybasicinfopo.setProvince(orgDto.getProvince());

            if (!StringUtils.isNullOrEmpty(orgDto.getCity())) {
                carfactorybasicinfopo.setCity(orgDto.getCity());
            }

            carfactorybasicinfopo.setZipCode(orgDto.getZipCode());
            carfactorybasicinfopo.setFax(orgDto.getFax());
            carfactorybasicinfopo.setHotLine(orgDto.getHotLine());
            carfactorybasicinfopo.setAddress(orgDto.getAddress());
            carfactorybasicinfopo.setDealerStatus(Integer.parseInt(orgDto.getDealerStatus()));
            carFactoryBasicinfoMapper.updateById(carfactorybasicinfopo);

            //公司组织
            //获取当前公司的组织id
            List<Map<String, Object>> orgList = carFactoryBasicinfoMapper.findOrganizationId(ids);

            if (!CommonUtils.isNullOrEmpty(orgList)) {
                int orgId = (int) orgList.get(0).get("ORG_ID");
                ComOrganizationPO comorganizationpo = comOrganizationMapper.selectById(orgId);
                comorganizationpo.setOrgName(orgDto.getDealerName());
                comorganizationpo.setStatus(Integer.parseInt(orgDto.getDealerStatus()));
                comOrganizationMapper.updateById(comorganizationpo);
			}
		}
	}
}
